package com.xing.action;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import lombok.Getter;
import lombok.Setter;

import org.apache.log4j.Logger;
import org.apache.struts2.json.annotations.JSON;
import org.springframework.stereotype.Controller;

import com.xing.model.EmployeeInfo;
import com.xing.model.ProjectInfo;
import com.xing.model.SubsidiaryInfo;
import com.xing.service.EmployeeService;
import com.xing.service.ProjectService;
import com.xing.service.SubsidiaryService;
import com.xing.util.QueryBuilder;

@Controller
public class ProjectAction extends BaseAction {
	/**
     * 
     */
	private static final long serialVersionUID = 1L;

	private static Logger logger = Logger.getLogger(ProjectAction.class);
	
	@Resource
	private ProjectService projectService;

	@JSON(serialize=false)
	public ProjectService getProjectService() {
		return projectService;
	}
	
	@Resource
	private SubsidiaryService subsidiaryService;

	@JSON(serialize=false)
	public SubsidiaryService getSubsidiaryService() {
		return subsidiaryService;
	}
	
	@Resource
	private EmployeeService employeeService;

	@JSON(serialize=false)
	public EmployeeService getEmployeeService() {
		return employeeService;
	}

	public String list() throws Exception {

		logger.info("do list method!");
		
		List<SubsidiaryInfo> subsidiaryList = subsidiaryService.getList();
		List<EmployeeInfo> employeeInfoList = employeeService.getList();
		
		getRequest().put("subsidiaryList", subsidiaryList);
		getRequest().put("employeeInfoList", employeeInfoList);
		
		return "list";
		
	}
	
	@Override
	public String dataGrid() {
		
		Map<String, Object> map = projectService.getListAndCount(buildQueryCondition(), getPage(), getRows());
		setDataGrid(map);
		
		return "dataGrid";
	} 
	
	public String addProject() throws Exception {
		
		logger.info("do addProject method!");
		
		ProjectInfo info = createProjectInfo();
		projectService.addProject(info);
		
		setMessage("msg", "新增成功.");
		
		return list();
	}
	
	public String modifyProject() throws Exception {
		
		ProjectInfo info = createProjectInfo();
		projectService.updateProject(info);
		
		setMessage("msg", "修改成功.");
		
		return list();
	}
	
	@JSON(serialize=false)
	public String getProject() throws Exception {
		
		logger.info("do getProject method!");
		
		jsonInfo = projectService.getProjectInfo(projectId);
		
		return "getObject";
	}
	
	public String deleteProject() throws Exception {
		
		logger.info("do deleteProject method!");
		
		projectService.deleteProject(getDeleteId());
		
		setMessage("msg", "删除成功.");
		
		return list();
	}

	private ProjectInfo createProjectInfo() {
		
		ProjectInfo info = new ProjectInfo();

		info.setProjectName(projectName);
		info.setProjectCode(projectCode);
		info.setSubsidiaryId(subsidiaryId);
		info.setBudgetAmount(budgetAmount);
		info.setContent(content);
		info.setStartTime(startTime);
		info.setEndTime(endTime);
		info.setManagerId(managerId);
		info.setRemark(remark);
		
		if (projectId > 0) {
			info.setProjectId(projectId);
			info.setUpdateTime(new Timestamp(System.currentTimeMillis()));
		} else {
			Timestamp createTime = new Timestamp(System.currentTimeMillis());
			info.setCreateTime(createTime);
			info.setUpdateTime(createTime);
		}
		
		return info;
	}
	
	
	private List<String> buildQueryCondition() {
		List<String> conditionList = new ArrayList<String>();
		
		QueryBuilder.buildQueryCondition(query_projectName, "c.projectName", QueryBuilder.LIKE, conditionList);
		QueryBuilder.buildQueryCondition(query_projectCode, "c.projectCode", QueryBuilder.LIKE, conditionList);
		QueryBuilder.buildQueryCondition(query_subsidiaryId, "c.subsidiaryId", QueryBuilder.EQUAL, conditionList);
		QueryBuilder.buildTimeQueryCondition(query_startPstartTime, query_endPstartTime, "c.startTime", conditionList);
		QueryBuilder.buildTimeQueryCondition(query_startPendTime, query_endPendTime, "c.endTime", conditionList);
		
		return conditionList;
	}
	
	
	private Map<String, Object> jsonInfo;
	@JSON(name="projectInfo")
	public Map<String, Object> getJsonInfo() {
		return jsonInfo;
	}

	@Getter @Setter private int projectId;
	@Getter @Setter private String projectName;
	@Getter @Setter private String projectCode;
	@Getter @Setter private int subsidiaryId;
	@Getter @Setter private double budgetAmount;
	@Getter @Setter private String content;
	@Getter @Setter private Timestamp startTime;
	@Getter @Setter private Timestamp endTime;
	@Getter @Setter private int managerId;
	@Getter @Setter private String remark;
	
	
	@Getter @Setter private String query_projectName;
	@Getter @Setter private String query_projectCode;
	@Getter @Setter private String query_subsidiaryId;
	@Getter @Setter private String query_subsidiaryName;
	@Getter @Setter private String query_startPstartTime;
	@Getter @Setter private String query_endPstartTime;
	@Getter @Setter private String query_startPendTime;
	@Getter @Setter private String query_endPendTime;

}
